package cn.shengchao.examstar.product.acl.adapters.repositories;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.shengchao.base.utils.SecureUtils;
import cn.shengchao.examstar.product.acl.ports.repositories.IProductUserOrderExpiredRepository;
import cn.shengchao.examstar.product.infrastructure.dao.TProductUserOrderExpiredDao;
import cn.shengchao.examstar.product.infrastructure.dao.po.TProductUserOrderExpired;
import cn.shengchao.examstar.rpc.product.base.ProductPropertyDto;
import cn.shengchao.examstar.rpc.product.base.ProductUserOrderExpiredDto;
import cn.shengchao.examstar.rpc.product.req.ProductUserOrderExpiredPageQuery;
import cn.shengchao.examstar.rpc.product.res.ProductUserOrderExpiredVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @program: examstarapi
 * @description: 产品用户购买有效期配置
 * @author: Xiong weiqiang
 * @create: 2025-06-27 20:10
 */
@Service
public class ProductUserOrderExpiredRepository implements IProductUserOrderExpiredRepository {
    @Autowired
    private TProductUserOrderExpiredDao productUserOrderExpiredDao;
    @Override
    public void saveOrderProductExpired(ProductUserOrderExpiredDto productUserOrderExpiredDto) {
        TProductUserOrderExpired tProductUserOrderExpired = new TProductUserOrderExpired();
        BeanUtil.copyProperties(productUserOrderExpiredDto,tProductUserOrderExpired);
        tProductUserOrderExpired.setStatus(0);
        productUserOrderExpiredDao.insert(tProductUserOrderExpired);
    }

    @Override
    public void recalSaveCourseExpired(Long userId, ProductPropertyDto productPropertyDto,  String orderId, Date orderTime) {
                TProductUserOrderExpired tProductUserOrderExpired = productUserOrderExpiredDao.selectOne(
                        Wrappers.lambdaQuery(TProductUserOrderExpired.class)
                                .eq(TProductUserOrderExpired::getUserId, userId)
                                .eq(TProductUserOrderExpired::getProductId, productPropertyDto.getCourseProductSettingDto().getProductId())
                                .eq(TProductUserOrderExpired::getProductType,  productPropertyDto.getCourseProductSettingDto().getProductType())
                                .orderByDesc(TProductUserOrderExpired::getOrderTime).last("limit 1")
                );
                Date date=orderTime;
                Integer days=productPropertyDto.getCourseProductSettingDto().getUnitPayDay();
                //需要积累
                if (tProductUserOrderExpired!=null&&tProductUserOrderExpired.getExpiredDay().getTime()>date.getTime()){
                    date=new Date(tProductUserOrderExpired.getExpiredDay().getTime()+days*24*60*60*1000L);
                }else{
                    date=new Date(date.getTime()+days*24*60*60*1000L);
                }
                TProductUserOrderExpired orderProductUserOrderExpired=new TProductUserOrderExpired();
                orderProductUserOrderExpired.setUserId(userId);
                orderProductUserOrderExpired.setOrderId(Long.valueOf(orderId));
                orderProductUserOrderExpired.setOrderTime(orderTime);
                orderProductUserOrderExpired.setProductId(productPropertyDto.getCourseProductSettingDto().getProductId());
                orderProductUserOrderExpired.setProductName(productPropertyDto.getProductName());
                orderProductUserOrderExpired.setProductType(productPropertyDto.getCourseProductSettingDto().getProductType());
                orderProductUserOrderExpired.setExpiredDay(date);
                orderProductUserOrderExpired.setBuyDayNum(days);
                orderProductUserOrderExpired.setRefThirdId(productPropertyDto.getCourseProductSettingDto().getRefThirdId());
                orderProductUserOrderExpired.setStatus(0);
                productUserOrderExpiredDao.insert(orderProductUserOrderExpired);
    }

    @Override
    public List<ProductUserOrderExpiredVo> pageList(ProductUserOrderExpiredPageQuery query) {
        if (StrUtil.isNotBlank(query.getUserPhone())){
            query.setUserPhone(SecureUtils
                    .encryptFieldByAes(query.getUserPhone()));
        }else {
            query.setUserPhone(null);
        }
        return productUserOrderExpiredDao.selectListByQuery(query);
    }
}